package com.yjs.app.trade.service.service.sys.impl;

import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.core.base.mybatis.dbutil.DynamicSwitchDataSource;
import com.yjs.app.trade.core.util.ClobUtil;
import com.yjs.app.trade.service.mapper.sys.QuestionMapper;
import com.yjs.app.trade.service.service.sys.QuestionService;

@Service("questionService")
@DynamicSwitchDataSource(dataSource="app")
public class QuestionServiceImpl implements QuestionService {
	@Autowired
	QuestionMapper questionMapper;

	/**
	 * 查询所有的常见问题
	 * @throws IOException 
	 * @throws SQLException 
	 */
	@Override
	public List<Map<String, Object>> findFAQPage() throws SQLException, IOException {
		List<Map<String, Object>> Listmap = (List<Map<String, Object>>) questionMapper.findFAQPage();
		for (Map<String, Object> map : Listmap) {
			for (Map.Entry<String,Object> entry: map.entrySet()) {
				if(entry.getValue() instanceof Timestamp){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					map.put(entry.getKey(), sdf.format(new Date(((Timestamp)entry.getValue()).getTime())));
				}else if(entry.getValue() instanceof Clob){
					map.put(entry.getKey(),ClobUtil.clobToString((Clob) entry.getValue()));
					}
				}
			}
		
		return Listmap;
	}

	@Override
	public Map<String, Object> getFAQById(String faqId) throws SQLException, IOException {
		Map<String, Object> map = questionMapper.getFAQById(faqId);
		Iterator<Map.Entry<String, Object>> it=map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String, Object> entry=it.next();
			if(entry.getValue() instanceof Timestamp){
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				map.put(entry.getKey(), sdf.format(new Date(((Timestamp)entry.getValue()).getTime())));
			}else if(entry.getValue() instanceof Clob){
				map.put(entry.getKey(),ClobUtil.clobToString((Clob) entry.getValue()));
				}
			}
		return map;
	}

	@Override
	public Map<String, Object> findServer() throws SQLException, IOException {
		return questionMapper.findServer();
	}

}
